package admission.ecnu.service;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import admission.ecnu.dao.teacherInfoDao;
import admission.ecnu.interfaces.IRnsService;
import admission.ecnu.mainservlet.RnsContext;
import admission.ecnu.object.DataManager;
import admission.ecnu.object.OTeacherInfo;

public class viewAllTeacher implements IRnsService {

	@Override
	public void executeService(RnsContext context) throws Exception {
		// TODO Auto-generated method stub
		JSONObject obj = context.getJsonRequestObject();
		if(!DataManager.USER_ID.equals(obj.get("key"))){
			JSONObject jsonReturn = new JSONObject();
			jsonReturn.put("returnCode", DataManager.RETURN_WRONGKEY);
			context.setJsonResponseObject(jsonReturn);
			return;
		}
		ArrayList<OTeacherInfo> teacjerList = (ArrayList<OTeacherInfo>) new teacherInfoDao().teacherSelect();
		
		JSONObject json = new JSONObject();
		json.put("returnCode", DataManager.RETURN_SUCCESSFUL);
		JSONArray jsonArray = new JSONArray();
		for(int i = 0; i<teacjerList.size(); i++){
			JSONObject objClass = new JSONObject();
			objClass.put("teacherId", teacjerList.get(i).getTeacherId());
			objClass.put("name", teacjerList.get(i).getName());
			objClass.put("special", teacjerList.get(i).getSpecial());
			objClass.put("title", teacjerList.get(i).getTitle());
			jsonArray.put(objClass);
		}
		json.put("returnValue", jsonArray);
		context.setJsonResponseObject(json);
	}
}